package utils;

import android.content.Context;
import android.util.Log;
import android.util.Printer;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.example.administrator.test.R;

/**
 * Created by Administrator on 2016/12/8.
 */
public class FixPosition {
    private static String locPlace;
    private static double longitude;
    private static double latitude;
    private static BDLocation mLoction;
    private static boolean isFirstLoc=true;
    public static void FixPosition(final TextureMapView mMapView, final BaiduMap mBaiduMap, final Context context, final GeoCoder geoCoder, final TextView tv_fragment_drivingservice_start){
        class MyLocationListener implements BDLocationListener{

            @Override
            public void onReceiveLocation(BDLocation location) {
                //mapView销毁后不再处理新接收的位置
                if (location == null|| mMapView==null){
                    return;
                }
                mLoction=location;
                MyLocationData locData=new MyLocationData.Builder()
                        .accuracy(mLoction.getRadius())
                        .direction(100).latitude(mLoction.getLatitude())
                        .longitude(mLoction.getLongitude()).build();
                mBaiduMap.setMyLocationData(locData);
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                longitude=ll.longitude;
                latitude=ll.latitude;
                //把经纬度转成具体地理位置的方法
                latlngToAddress(ll,geoCoder,tv_fragment_drivingservice_start,context);
                MapStatus.Builder builder = new MapStatus.Builder();
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
                MapStatusUpdate mapStatus = MapStatusUpdateFactory.newLatLngZoom(ll, 18);
                mBaiduMap.setMapStatus(mapStatus);
            }
        }
        MyLocationListener myListener=new MyLocationListener();
        mBaiduMap.setMyLocationEnabled(true);//开启定位图层

//        LocationClient locationClient=new LocationClient(context);//定位对象
//        LocationClientOption option = new LocationClientOption();
//        option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
//        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
//        //可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
//        int span=1000;
//        option.setScanSpan(span);
//        option.setIgnoreKillProcess(false);//**
//        locationClient.setLocOption(option);
        LocationClient locationClient=new LocationClient(context);//定位对象
        locationClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setAddrType("all");
        locationClient.setLocOption(option);
        //第四步:启动定位功能
        locationClient.start();
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                return true;
            }
        });

//        locationClient.registerLocationListener(new BDLocationListener() {
//            @Override
//            public void onReceiveLocation(BDLocation bdLocation) {
//                //根据定位回传的结果获取当前的经纬度对象
//                    LatLng latLng=new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
//                    longitude=latLng.longitude;
//                    latitude=latLng.latitude;
//                    Log.i("最开始tagtag","-----------------------------"+"纬度："+latitude+"经度："+longitude+"----------------------");
//                    //把经纬度转成具体地理位置的方法
//                    latlngToAddress(latLng,geoCoder,tv_fragment_drivingservice_start,context);
//                    //根据获取的定位经纬度对象  设置响应的标记显示到地图上
//                    MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
//                    //根据当前的经纬度位置设置相应的marker标记点
//                    BitmapDescriptor bm= BitmapDescriptorFactory.fromResource(R.mipmap.icon_marka);
//                    MarkerOptions options=new MarkerOptions().icon(bm).position(latLng);
//                    //将图层添加到地图上
//                    if(mBaiduMap!=null){
//                        mBaiduMap.addOverlay(options);
//                    }
//                    mBaiduMap.animateMapStatus(msu);
//
//            }
//        });

    }

    private static void latlngToAddress(LatLng latlng, GeoCoder geoCoder, final TextView tv_fragment_drivingservice_start, final Context context) {
        // 设置反地理经纬度坐标,请求位置时,需要一个经纬度
        geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(latlng));
        //设置地址或经纬度反编译后的监听,这里有两个回调方法,
        geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            //经纬度转换成地址
            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
                if (result == null ||  result.error != SearchResult.ERRORNO.NO_ERROR) {
//                    Toast.makeText(context, "找不到该地址!",Toast.LENGTH_SHORT).show();
                }
                tv_fragment_drivingservice_start.setText(result.getAddress());
                locPlace=result.getAddress();
            }
            //地址转换成经纬度
            @Override
            public void onGetGeoCodeResult(GeoCodeResult result) {
                // 详细地址转换成经纬度
                String address=result.getAddress();
            }
        });
    }
    public static double getStartLatitude(){
        return latitude;
    }
    public static double getStartLongitude(){
        return longitude;
    }
    public static String getLocPlace(){
        return locPlace;
    }

}
